package demo28;

/**
 * 线程安全的单例模式：
 * 阅读文章：https://ww.cnblogs.com/xudong-bupt/p/3433643.html
 * 更好的是采用下面的方式，既不用加锁，也能实现懒加载
 */

public class Singleton4 {
    private Singleton4() {
        System.out.println("初始化Singleton4..");
    }
    private static class Inner { //静态内部类
        static{
            System.out.println("Inner静态内部类加载了...");
        }
        private static Singleton4 s = new Singleton4();
    }
    private static Singleton4 getSingle() {
        System.out.println("获取Single实例...");
        return Inner.s;
    }


    public static void main(String[] args) {
        Runnable runnable = () -> System.out.println(Singleton4.getSingle());
        new Thread(runnable,"t1").start();
        new Thread(runnable,"t2").start();
    }
}
